package cn.egame.extraction.multithread.service.filter;

import java.util.ArrayList;
import java.util.List;

import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.extraction.multithread.pojo.EfsInfo;
import cn.egame.extraction.multithread.service.CrawlerThread;
import cn.egame.extraction.multithread.util.ConstVar_EFS;
import cn.egame.extraction.multithread.util.EnumType_EFS.InputErrorFileAppendType;
import cn.egame.interfaces.fl.FileUsedType;
import cn.egame.interfaces.fl.FileUtils;

public class FilePathListFilter extends Filter {

	@Override
	public void handleRequest(ChainObject chainObject) throws ExceptionCommonBase{
		EfsInfo efsInfo = (EfsInfo) chainObject.get("efsInfo");
		CrawlerThread currentThread = (CrawlerThread) chainObject.get("currentThread");
		if(efsInfo!=null
				&& currentThread!=null){
			String localfilePath = null;
			if(efsInfo.getEfs_type().matches("^\\d+$")){
				int efsTypeNum = Utils.toInt(efsInfo.getEfs_type(), 0);
				try {
					List<String> filePathList = new ArrayList<String>();
					localfilePath = FileUtils.getFilePath(FileUsedType.lookup(efsTypeNum), 
							efsInfo.getEfs_id(), efsInfo.getFile_name());
					filePathList.add(localfilePath);
					//以下三种图片文件类型需要拉取压缩的多份
					if (efsTypeNum == FileUsedType.GAME_PHOTO) {
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.GAME_PHOTO_BIG), 
                                efsInfo.getEfs_id(), efsInfo.getFile_name()));
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.GAME_PHOTO_MID), 
                                efsInfo.getEfs_id(), efsInfo.getFile_name()));
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.GAME_PHOTO_SMALL), 
                            efsInfo.getEfs_id(), efsInfo.getFile_name()));
                    } else if (efsTypeNum == FileUsedType.HEAD_PHOTO) {
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.HEAD_SML), efsInfo.getEfs_id(),
                                efsInfo.getFile_name()));
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.HEAD_MID), efsInfo.getEfs_id(),
                                efsInfo.getFile_name()));
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.HEAD_BIG), efsInfo.getEfs_id(),
                                efsInfo.getFile_name()));
                    } else if (efsTypeNum == FileUsedType.ALBUM_PHOTO) {
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.ALBUM_PHOTO_SML), efsInfo.getEfs_id(),
                                efsInfo.getFile_name()));
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.ALBUM_PHOTO_MID), efsInfo.getEfs_id(),
                                efsInfo.getFile_name()));
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.ALBUM_PHOTO_BIG), efsInfo.getEfs_id(),
                                efsInfo.getFile_name()));
                    } else if (efsTypeNum == FileUsedType.GAME_VIEW) {
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.GAME_VIEW_BIG), 
                                efsInfo.getEfs_id(), efsInfo.getFile_name()));
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.GAME_VIEW_MID), 
                                efsInfo.getEfs_id(), efsInfo.getFile_name()));
                        filePathList.add(FileUtils.getFilePath(FileUsedType.lookup(FileUsedType.GAME_VIEW_SMALL), 
                                efsInfo.getEfs_id(), efsInfo.getFile_name()));
                    }

					
					chainObject.put("filePathList", filePathList);
					getSuccessor().handleRequest(chainObject);
					
				}catch (Exception e) {
					currentThread.errorSyncIdLog.error("id为"+efsInfo.getEfs_id()+"的文件拉取失败, 对应的url为:"+ConstVar_EFS.DNS_HOST+localfilePath);
					currentThread.getFileService().writeBackToFilePath(ConstVar_EFS.ERROR_IDS_FILE_PATH_500,
							InputErrorFileAppendType.rewrite, efsInfo, 1);
					currentThread.getFileService().fetchOkAndWriteBack(
							currentThread.getFilename(), efsInfo);
				}
			}
		}
	}
}
